package com.sxfq.loansupermarket.backsystem.modular.weixin.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.GunsApplication;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.TcInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.ReservationsInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.SearchReservationsInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.service.IReservationsService;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.service.IConsultantService;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.service.ITcRevisitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 微信开发者接入
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/tcPatientsCentral")
public class TcPatientsCentralController extends BaseController {
    private String PREFIX = "/weixin/app/";

    protected final static Logger logger = LoggerFactory.getLogger(GunsApplication.class);

    @Autowired
    private SqlMapper sqlMapper;
    @Autowired
    IConsultantService tcservice;
    @Autowired
    ITcRevisitService tcRevisitService;

    /**
     * tc患者列表加载
     */
    @RequestMapping(value = "/getTcPatient", method = RequestMethod.POST)
    @ResponseBody
    public List<ConsultantInfo> getTcPatient(String startArrivalTime,String endArrivalTime,String reach) {
        SearchConsultantInfo info=new SearchConsultantInfo();
        Page<ConsultantInfo> page = new Page<>();
        info.setStartIndex(0);
        info.setLimitIndex(50);
        info.setStartArrivalTime(startArrivalTime);
        info.setEndArrivalTime(endArrivalTime);
        info.setReach(reach);
        page.setCurrent(0);
        page.setSize(50);
        List<ConsultantInfo> list = tcservice.selectPageListByQueryGetMap(page, info);
        return list;
    }

    /**
     * APP - tc
     * 点击患者跳转页面 - 详情
     */
    @RequestMapping("/getTcPatient_details/{customerId}")
    public String getTcPatient_details(@PathVariable String customerId,Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        String sql="select (CASE a.customer_sex " +
                " WHEN 1 THEN " +
                "  '男' " +
                "  WHEN 2 THEN " +
                "  '女' " +
                "  ELSE " +
                "  '未知' " +
                "  END " +
                "  ) as sex ," +
                "( " +
                " CASE a.customer_sex " +
                " WHEN 1 THEN " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait01.png' " +
                " WHEN 2 THEN " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait02.png' " +
                " ELSE " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait03.png' " +
                " END " +
                ") as avatar ,"+
                "a.* from "+ShiroKit.getUser().getCustomer_info()+" a where id ='"+customerId+"'";
        ReservationsInfo info=sqlMapper.selectOne(sql,ReservationsInfo.class);
        model.addAttribute("info",info);
        return PREFIX + "TC/patient/patient_details.html";
    }

    /**
     * APP - tc
     * 点击回访跳转页面 - 回访详情
     */
    @RequestMapping("/tcPatient_returnVisit/{reservationsId}")
    public String patient_returnVisit(@PathVariable String reservationsId ,Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        //回访记录
        String sql4 = "select r.id,r.reservations_id,r.customer_id,r.create_name,r.revisit,r.revisit_time,r.create_time,r.update_time," +
                "r.outpatient_name,r.revisit_content,r.tc_name,r.revisit_status,r.remarks,r.consumption_status,r.follow_status from return_tc_visit_info r " +
                " where r.reservations_id ='" + reservationsId + "' order by revisit_time desc";
        List<TcRevisitsInfo> tcRevisitInfoList = sqlMapper.selectList(sql4, TcRevisitsInfo.class);
        model.addAttribute("tcRevisitInfoList",tcRevisitInfoList);
        model.addAttribute("reservationsId",reservationsId);
        return PREFIX + "TC/patient/patient_returnVisit.html";
    }

    /**
     * APP - tc
     * 点击回访跳转页面 - 回访详情
     */
    @RequestMapping("/tcPatient_returnVisitTwo/{reservationsId}")
    public String patient_returnVisitTwo(@PathVariable String reservationsId ,Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        //回访记录
        String sql4 = "select r.id,r.reservations_id,r.customer_id,r.create_name,r.revisit,r.revisit_time,r.create_time,r.update_time," +
                "r.outpatient_name,r.revisit_content,r.tc_name,r.revisit_status,r.remarks,r.consumption_status,r.follow_status from return_tc_visit_info r " +
                " where r.reservations_id ='" + reservationsId + "' order by revisit_time desc";
        List<TcRevisitsInfo> tcRevisitInfoList = sqlMapper.selectList(sql4, TcRevisitsInfo.class);
        model.addAttribute("tcRevisitInfoList",tcRevisitInfoList);
        return PREFIX + "TC/patient/patient_returnVisit_Two.html";
    }

    //加载Tc回访列表
    @RequestMapping(value = "/tc_returnVisit_list", method = RequestMethod.POST)
    @ResponseBody
    public List<TcRevisitInfo> tc_returnVisit_list(String startRevisitTime,String endRevisitTime,Integer revisitStatus,
                                                   String revisit,String followStatus,String consumptionStatus,HttpServletRequest request) {
        SearchTcRevisitInfo info=new SearchTcRevisitInfo();
        Page<TcRevisitInfo> page =  new Page<>();
        info.setStartIndex(0);
        info.setLimitIndex(50);
        info.setRevisitStatus(revisitStatus);
        info.setStartRevisitTime(startRevisitTime);
        info.setEndRevisitTime(endRevisitTime);
        info.setRevisit(revisit);
        info.setFollowStatus(followStatus);
        info.setConsumptionStatus(consumptionStatus);
        page.setCurrent(0);
        page.setSize(50);
        List<TcRevisitInfo> list = tcRevisitService.selectPageListByRevisit(page, info);
        return list;
    }

    //跳转到添加回访页面--Tc
    @RequestMapping("/tc_patient_addVisit/{reservationsId}")
    public String tc_patient_addVisit(@PathVariable String reservationsId ,Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getUser().getTcList() == null) {
            String getListSql = " select a.id as id,a.account as tcName from user a left join  dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on a.roleid=c.id " +
                    " where b.abbreviation='"+tableHead+"' and c.name like CONCAT('%','tc','%') ";
            List<TcInfo> tcList =sqlMapper.selectList(getListSql,TcInfo.class);
            ShiroKit.getUser().setTcList(tcList);
        }
        String sql="select (CASE a.customer_sex " +
                " WHEN 1 THEN " +
                "  '男' " +
                "  WHEN 2 THEN " +
                "  '女' " +
                "  ELSE " +
                "  '未知' " +
                "  END " +
                "  ) as sex ," +
                "( " +
                " CASE a.customer_sex " +
                " WHEN 1 THEN " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait01.png' " +
                " WHEN 2 THEN " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait02.png' " +
                " ELSE " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait03.png' " +
                " END " +
                ") as avatar ,"+
                "a.* from "+ShiroKit.getUser().getCustomer_info()+" a "+
                "where reservations_id ='"+reservationsId+"' order by arrival_time desc limit 1";
        CustomerInfo info=sqlMapper.selectOne(sql,CustomerInfo.class);
        model.addAttribute("info",info);
        model.addAttribute("reservationsId",reservationsId);
        model.addAttribute("tcList", ShiroKit.getUser().getTcList());
        return PREFIX + "TC/patient/patient_addVisit.html";
    }


    /**
     * APP - tc
     * 点击回访跳转页面 - 回访详情 编辑
     */
    @RequestMapping("/tc_patient_editVisit/{reservationsId}/{id}")
    public String tc_patient_editVisit(@PathVariable String reservationsId,@PathVariable String id,Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getUser().getTcList() == null) {
            String getListSql = " select a.id as id,a.account as tcName from user a left join  dept b on a.deptid like CONCAT('%',b.id,',','%') left join role c on a.roleid=c.id " +
                    " where b.abbreviation='"+tableHead+"' and c.name like CONCAT('%','tc','%') ";
            List<TcInfo> tcList =sqlMapper.selectList(getListSql,TcInfo.class);
            ShiroKit.getUser().setTcList(tcList);
        }
        String sql="select (CASE a.customer_sex " +
                " WHEN 1 THEN " +
                "  '男' " +
                "  WHEN 2 THEN " +
                "  '女' " +
                "  ELSE " +
                "  '未知' " +
                "  END " +
                "  ) as sex ," +
                "( " +
                " CASE a.customer_sex " +
                " WHEN 1 THEN " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait01.png' " +
                " WHEN 2 THEN " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait02.png' " +
                " ELSE " +
                " 'https://pynnn.oss-cn-hangzhou.aliyuncs.com/weixin-img/app/portrait03.png' " +
                " END " +
                ") as avatar ,"+
                "a.* from "+ShiroKit.getUser().getCustomer_info()+" a "+
                "where reservations_id ='"+reservationsId+"' order by arrival_time desc limit 1";
        CustomerInfo info=sqlMapper.selectOne(sql,CustomerInfo.class);
        String sql1="select * from return_tc_visit_info where id ='"+id+"'";
        TcRevisitInfo tcRevisit=sqlMapper.selectOne(sql1,TcRevisitInfo.class);
        model.addAttribute("tcRevisit",tcRevisit);
        model.addAttribute("info",info);
        model.addAttribute("reservationsId",reservationsId);
        model.addAttribute("tcList", ShiroKit.getUser().getTcList());
        return PREFIX + "TC/patient/patient_editVisit.html";
    }

    /**
     * 计划回访 修改
     */
    @RequestMapping(value = "/editVisit1", method = RequestMethod.POST)
    @ResponseBody
    public int editVisit1(String customerId,String followStatus, String consumptionStatus, String revisitContent, String revisitTime,
                          String remarks, String reservationsId,String revisit,String id) {
        String updatesql = "update " + ShiroKit.getUser().getCustomer_info() + " set follow_status='" + followStatus + "',consumption_status='" + consumptionStatus + "' " +
                "where reservations_id='" + customerId + "'";
        String sql = "update return_tc_visit_info set create_name='" + ShiroKit.getUser().getAccount()
                + "',revisit_time='" + revisitTime + "',revisit_content='" + revisitContent + "',remarks='" + remarks + "',revisit='" + revisit + "'," +
                "revisit_status='1',update_time='"+DateTimeKit.now()+"',follow_status='"+followStatus+"',consumption_status='"+consumptionStatus+"' " +
                "where reservations_id='" + reservationsId + "' and id='" + id + "'";
        sqlMapper.update(updatesql);
        return sqlMapper.update(sql);
    }

    /**
     * 回访完成 修改
     */
    @RequestMapping(value = "/editVisit2", method = RequestMethod.POST)
    @ResponseBody
    public int editVisit2(String customerId,String followStatus, String consumptionStatus, String revisitContent, String revisitTime,
                          String remarks, String reservationsId, String revisit,String id) {
        String updatesql = "update " + ShiroKit.getUser().getCustomer_info() + " set follow_status='" + followStatus + "',consumption_status='" + consumptionStatus + "' " +
                "where reservations_id='" + customerId + "'";
        String sql = "update return_tc_visit_info set create_name='" + ShiroKit.getUser().getAccount()
                + "',revisit_time='" + revisitTime + "',revisit_content='" + revisitContent + "',remarks='" + remarks + "',revisit='" + revisit + "'," +
                "revisit_status='2',update_time='"+DateTimeKit.now()+"',follow_status='"+followStatus+"',consumption_status='"+consumptionStatus+"' " +
                "where reservations_id='" + reservationsId + "' and id='" + id + "'";
        sqlMapper.update(updatesql);
        return sqlMapper.update(sql);
    }

}